
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: SetField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.util.Set;

import org.dellroad.sidekar.query.QuerySet;

/**
 * A {@link Field} with set semantics, where the elements of the set are {@link SimpleField}s.
 *
 * @param <E> element type
 * @see org.dellroad.sidekar.annotation.SidekarSet
 */
public final class SetField<E> extends CollectionField<E> {

    SetField(String name, Entity<?> entity, SimpleField<E> elementField) {
        super(name, entity, Set.class, elementField);
    }

    /**
     * @throws DetachedItemException {@inheritDoc}
     * @throws IllegalArgumentException {@inheritDoc}
     * @throws IllegalStateException {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     * @throws org.dellroad.sidekar.util.SidekarException {@inheritDoc}
     */
    @Override
    public QuerySet<E> get(Item<?> item) {
        this.validate();
        item.sidekarValidate();
        return this.getDatastore().setFieldAccess(this.getEntity(), item.getSidekarId(), this);
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseSetField(this);
    }
}

